package com.zhengke.service.placeOrder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhengke.common.dao.CommonDao;
import com.zhengke.common.object.Result;
import com.zhengke.dao.placeOrder.xdMapper;
import com.zhengke.pojo.MallServiceGoods01;
import com.zhengke.pojo.ToBuyMsg;
import com.zhengke.pojo.TvOrder;
import com.zhengke.pojo.TvOrderGoods;
import com.zhengke.pojo.TvUser;
@Service
public class xdServiceImpl implements xdService {

	@Autowired
	private xdMapper dao;
	
    @Autowired
    private CommonDao wxdao;
	
	@Override
	public Result findGoods(String areaId, String goodsName, String type, int start, int size) {

		Map<String, Object> map = new HashMap<String, Object>();
		
		map.put("pointId", areaId);
		map.put("goodsName", goodsName);
		map.put("type", type);
		PageHelper.startPage(start, size, true);
		List<Map<String, Object>> goodsList = dao.findGoods(map);
		
		PageInfo<Map<String, Object>> pageInfo = new PageInfo<Map<String,Object>>(goodsList);
		  Result rs = new Result();
	        rs.setCode("0");
	        rs.setCount(pageInfo.getTotal());
	        rs.setData(goodsList);
	        return rs;
	}

	@Override
	public List<ToBuyMsg> findGoodsDetile(String goodsId, String goodsType) {

		Map<String, Object> map = new HashMap<String,Object>();
		map.put("goodsId", goodsId);
		map.put("goodsType", goodsType);
		
		return dao.findGoodsDetile(map);
	}

	/**
	 *  创建订单
	 *  保存用户信息
	 * @throws Exception 
	 */
	
	@Override
	public void createOrder(TvOrder to, TvOrderGoods tog, MallServiceGoods01 msg, TvUser user) throws Exception {
		// TODO Auto-generated method stub
		  String orderSn = to.getOrderSn();
	        //插入一条tvOrder数据
	        int result = 0;
			try {
				result = (int) wxdao.save("orderMapper.insertOneOrder", to);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	        if (result > 0) {//表明插入成功
	            int ordId = 0;
				try {
					ordId = (int) wxdao.findForObject("orderMapper.selectOrderId", orderSn);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	            tog.setOrderId(ordId);
	            //插入一条TvOrderGoods数据
	            wxdao.save("ddgl060Mapper.insertOneTog", tog);
	            //更新一条MallServiceGoods库存和订单限额
	            dao.updateMallServiceGoods(msg);
	            //修改user积分，参数 userCardNumber
	            wxdao.update("jfdhMapper03.updateByPrimaryKey", user);
	        }
		
		
		
		
	}

}
